﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.View;

namespace Benefit.Service.View
{
    /// <summary>
    /// 交易员日资金情况
    /// </summary>
    public class ServiceOperatorDayMoney : Benefit.Interface.View.IOperatorDayMoney
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorDayMoney(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 得到交易日账户信息
        /// </summary>
        /// <param name="accountMoneys"></param>
        /// <returns></returns>
        public OperatorDayMoney GetOperatorDayMoney(List<Models.Day.AccountDayMoney> accountMoneys, int operatorId, int tradeHistoryId)
        {
            if (accountMoneys.Count > 0)
            {
                OperatorDayMoney money = new OperatorDayMoney();
                money.OperatorId = operatorId;
                money.TradeHistoryId = tradeHistoryId;
                money.AppendMargin = accountMoneys.Sum(a => a.AppendMargin);
                money.AvailableMoney = accountMoneys.Sum(a => a.AvailableMoney);
                money.BaseMargin = accountMoneys.Sum(a => a.BaseMargin);
                money.CloseProfit = accountMoneys.Sum(a => a.CloseProfit);
                money.Commission = accountMoneys.Sum(a => a.Commission);
                money.DeliveryCommission = accountMoneys.Sum(a => a.DeliveryCommission);
                money.DeliveryMargin = accountMoneys.Sum(a => a.DeliveryMargin);
                money.Deposit = accountMoneys.Sum(a => a.Deposit);
                money.DynamicMoney = accountMoneys.Sum(a => a.DynamicMoney);
                money.FrozenCommission = accountMoneys.Sum(a => a.FrozenCommission);
                money.FrozenMargin = accountMoneys.Sum(a => a.FrozenMargin);
                money.LongPosition = accountMoneys.Sum(a => a.LongPosition);
                money.Margin = accountMoneys.Sum(a => a.Margin);
                money.Mortgage = accountMoneys.Sum(a => a.Mortgage);
                money.PositionProfit = accountMoneys.Sum(a => a.PositionProfit);
                money.Premortgage = accountMoneys.Sum(a => a.Premortgage);
                money.PresetTlePosit = accountMoneys.Sum(a => a.PresetTlePosit);
                money.RiskRate = accountMoneys.Sum(a => a.RiskRate);
                money.SettlePosit = accountMoneys.Sum(a => a.SettlePosit);
                money.ShortPosition = accountMoneys.Sum(a => a.ShortPosition);
                money.WithDraw = accountMoneys.Sum(a => a.WithDraw);
                money.WithDrawQuota = accountMoneys.Sum(a => a.WithDrawQuota);
                return money;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 初始化操作员资金信息
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        public void InitOperatorDayMoney(int tradeHistoryId)
        {
            var operators = db.Operators.ToList();
            foreach (Models.Sys.Operator op in operators)
            {
                List<Models.Day.AccountDayMoney> moneys = new List<Models.Day.AccountDayMoney>();
                Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
                Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
                List<Models.Sys.Account> accounts = idoa.GetDayOperatorAccounts(ith.GetDateTimeFromTradeHistoryId(tradeHistoryId), op.Id);
                foreach (Models.Sys.Account a in accounts)
                {
                    Interface.Day.IAccountDayMoney iadm = new Day.ServiceAccountDayMoney(db);
                    Models.Day.AccountDayMoney money = iadm.GetAccountDayMoney(tradeHistoryId, a.Id);
                    moneys.Add(money);
                }
                OperatorDayMoney _money = GetOperatorDayMoney(moneys, op.Id, tradeHistoryId);
                if (_money != null)
                {
                    db.OperatorDayMoney.Add(_money);
                }
            }
            db.SaveChanges();
        }

        public void Delete(int tradeHistoryId)
        {
            var query = db.OperatorDayMoney.Where(a => a.TradeHistoryId == tradeHistoryId);
            foreach (OperatorDayMoney m in query)
            {
                db.OperatorDayMoney.Remove(m);
            }
            db.SaveChanges();
        }

        public OperatorDayMoney GetOperatorDayMoney(int operatorid, int tradeHistoryId)
        {
            var query = db.OperatorDayMoney.Where(a => a.OperatorId == operatorid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return new OperatorDayMoney();
            }
        }
    }
}
